package www.dianbaozu.com.ui.my

import android.annotation.SuppressLint
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentContractBinding
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.ContractEntity
import www.wen.com.common.lib.extension.finishMyself
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter


/**
 * @author WEN
 * @Description: 关于我们
 * @date 2020//25 17:48
 */

@AndroidEntryPoint
class ContractFragment : BaseLoadingFragment<FragmentContractBinding>() {

    lateinit var mContractEntity:ContractEntity

    lateinit var mTitleBarUniversalAdapter:TitleBarUniversalAdapter

    private val mBaseApiViewModel: ContractViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter =
        TitleBarUniversalAdapter(requireActivity(), action = ::titleAction).apply {
            mTitleBarUniversalAdapter = this
        }

    override fun getLayoutId(): Int = R.layout.fragment_contract

    override fun lazyInitView() {
        webSetting()
        initializeUI()
    }

    override fun actionLeft() {
        if(parentFragment == null){
            finishMyself()
        }else{
            super.actionLeft()

        }
    }

    private fun initializeUI() {
        mContractEntity = requireArguments().getParcelable(BUNDLE_DATA)!!
        mTitleBarUniversalAdapter.mViewHolder.titleView.text = mContractEntity.title
        when(mContractEntity.type){
            "帮助中心"->{
                dataBinding.webView.loadDataWithBaseURL(null,mContractEntity.content?:"", "text/html" , "utf-8", null)
            }
            "隐私政策",
            "关于我们",
            "用户协议"->{
                launchRequest()
            }
        }
    }

    private fun launchRequest() {
        mBaseApiViewModel.requestProtocol(mContractEntity.type){
            if(it.content.isNotEmpty()){
                dataBinding.webView.loadDataWithBaseURL(null, it.content[0].content, "text/html" , "utf-8", null)
            }
        }
    }

    override fun onReload() { launchRequest() }

    @SuppressLint("SetJavaScriptEnabled")
    private fun webSetting() {
        val settings: WebSettings = dataBinding.webView.settings
        settings.javaScriptEnabled = true
        settings.useWideViewPort = true
        settings.loadWithOverviewMode = true
        settings.setSupportZoom(false)
        settings.builtInZoomControls = false
        settings.blockNetworkImage = true
        settings.allowFileAccess = false
        settings.domStorageEnabled = true
        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        dataBinding.webView.webViewClient = WebViewClient()
        dataBinding.webView.webChromeClient = WebChromeClient()
    }

}



